package canPlaceFlowers;

public class Solution {
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        // 判断每个 1 - 1 区间最多能装多少个(n-1)/2

        // 处理 0 - 0区间 (n-1)/2

//        int l = 0;
//        int r = 1;
//        while(r<flowerbed.length){
//            while(flowerbed[r]!=1){
//                r++;
//            }
//            int zeroCount = r - l -flowerbed[l];
//            if(l == 0&& flowerbed[l] == 0){
//                zeroCount++;
//            }
//            if(r == flowerbed.length&&flowerbed[r-1] == 0 ){
//                zeroCount++;
//            }
//            l = r++;
//            n-=(zeroCount-1)/2;
//            if(n<=0){
//                return true;
//            }
//        }
//        return false;
        if(flowerbed.length==1){
            return flowerbed[0]<n;
        }
        int count = 0;
        for (int i = 0; i < flowerbed.length; i++) {
            if(i == 0){
                if(flowerbed[i] == 0&& flowerbed[i+1] == 0){
                    count++;
                    flowerbed[i] = 1;
                }
            }else if( i == flowerbed.length-1){
                if(flowerbed[i] == 0&& flowerbed[i-1] == 0){
                    count++;
                }
            }else {
                if(flowerbed[i] == 0&& flowerbed[i-1] ==0 &&flowerbed[i+1] == 0){
                 flowerbed[i] = 1;
                 count++;
                }
            }
        }
        return count>=n;
        //  0 0 1 0 1 0 0 0 0 0 1 0 0 1 0 1 0 0
    }
}
